//
//  FPNearbyTableViewController.m
//  FilmParadise
//
//  Created by lanou3g on 15/7/14.
//  Copyright (c) 2015年 DLYgroup. All rights reserved.
//

#import "FPNearbyTableViewController.h"
#import "FPCinemaTableViewCell.h"
#import "BMapKit.h"
#import "FPLocation.h"

@interface FPNearbyTableViewController ()<BMKLocationServiceDelegate,BMKPoiSearchDelegate>
{
    float _currentLatirude;
    float _currentLongitude;
}

@property (nonatomic, strong) BMKPoiSearch *poisearch;
@property (nonatomic, strong) BMKLocationService *locService;
@property (nonatomic, strong) NSMutableArray *nearbyCNameArray;
@property (nonatomic, strong) NSMutableArray *cinemaAddressArray;
@property (nonatomic, strong) NSMutableArray *currentDistanceArray;
@end


@implementation FPNearbyTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // 适配
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 7.0) {
        self.navigationController.navigationBar.translucent = NO;
    }
    
    [self getMothed];
    
}
- (void)getMothed
{
    _poisearch = [[BMKPoiSearch alloc] init];
    _poisearch.delegate = self;
    
    _locService = [[BMKLocationService alloc] init];
    _locService.delegate = self;
    [_locService startUserLocationService];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   
    [self search];
   
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    _locService.delegate = nil;
    _poisearch.delegate = nil;
    [_locService stopUserLocationService];
//    [_mapView viewWillAppear];
//    _poisearch = [[BMKPoiSearch alloc] init];
//    _mapView.delegate = self;// 此处不用的时候要置为nil,否则影响内存释放
//    _locService.delegate = self;
//    _poisearch.delegate = self;
}

// 用户位置更新后,会调用此函数
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    _currentLatirude = userLocation.location.coordinate.latitude;
    _currentLongitude = userLocation.location.coordinate.longitude;
    NSLog(@"%f------%f",_currentLatirude,_currentLongitude);
    NSLog(@"用户更新了位置");
}
// 地图view停止定位后,会调用此函数
- (void)didStopLocatingUser
{
    NSLog(@"stop locate");
}

// 定位失败后,会调用此函数
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"location error");
}

- (void)search
{
    
    BMKCitySearchOption *citySearchOption = [[BMKCitySearchOption alloc] init];
    citySearchOption.pageIndex = 10;
    citySearchOption.city = [FPLocation shareInstance].cityLocal;
    NSLog(@"城市:%@",citySearchOption.city);
    citySearchOption.keyword = @"电影院";
    
    BOOL flag = [self.poisearch poiSearchInCity:citySearchOption];
    if (flag) {
        NSLog(@"城市内检索发送成功");
    }else
    {
        NSLog(@"城市内检索发送失败");
    }
}

/*===========================================================================*/
#pragma mark -
#pragma mark implement BMKSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
{
    
    if (error == BMK_SEARCH_NO_ERROR) {
        NSMutableArray *annotations = [NSMutableArray array];
        self.currentDistanceArray   = [NSMutableArray array];
        self.nearbyCNameArray        = [NSMutableArray array];
        self.cinemaAddressArray     = [NSMutableArray array];
        for (int i = 0; i < result.poiInfoList.count; i++) {
            BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
            BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
            item.coordinate = poi.pt;
            item.title = poi.name;
            BMKMapPoint point1 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake(_currentLatirude, _currentLongitude));
            BMKMapPoint point2 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake(poi.pt.latitude, poi.pt.longitude));
            CLLocationDistance distance = BMKMetersBetweenMapPoints(point1, point2);
            NSLog(@"-=-=-=-=-==%f",distance/1000);
            [self.currentDistanceArray addObject:[NSString stringWithFormat:@"%f",distance]];
            [self.nearbyCNameArray addObject:poi.name];
            [self.cinemaAddressArray addObject:poi.address];
            NSLog(@"%ld------",(unsigned long)_cinemaAddressArray.count);
            NSLog(@"--------%@",poi.name);
            [annotations addObject:item];
        }
        [self.tableView reloadData];
    } else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
        NSLog(@"起始点有歧义");
    } else {
        // 各种情况的判断。。。
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nearbyCNameArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifier = @"cell";
    [tableView registerClass:[FPCinema_TableViewCell class] forCellReuseIdentifier:cellIdentifier];
    FPCinema_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    NSLog(@"%ld",(unsigned long)_cinemaAddressArray.count);
    
    cell.cinemaNameLabel.text   = _nearbyCNameArray[indexPath.row];
    cell.addressLabel.text      = _cinemaAddressArray[indexPath.row];
    cell.priceLabel.text        = _currentDistanceArray[indexPath.row];
//
    

    
    return cell;
}


@end
